package tang.server;

import tang.common.OpCode;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.Set;

/**
 * @author Tang
 * @classname ZkServiceMain
 * @description [  ]
 * @date 2021/2/23 20:53
 */
public class ZkServiceTest {

    public static void main(String[] args) throws Exception {

        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        Selector selector = Selector.open();

        serverSocketChannel.configureBlocking(false);
        serverSocketChannel.bind(new InetSocketAddress("127.0.0.1", 9000));
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

        System.out.println("服务器启动中....");
        while (true) {
            selector.select();
            Set<SelectionKey> selectionKeys = selector.selectedKeys();
            selectionKeys.forEach(selectionKey -> {
                try {
                    if (selectionKey.isAcceptable()) {
                        SocketChannel socketChannel = serverSocketChannel.accept();
                        socketChannel.configureBlocking(false);
                        socketChannel.register(selector, SelectionKey.OP_READ);
                        System.out.println("新进来一个连接..." + socketChannel.hashCode());
                    } else if (selectionKey.isConnectable()) {
                        System.out.println("客户端与服务器连接建立成功...");
                    } else if (selectionKey.isReadable()) {
                        SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
                        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
                        socketChannel.read(byteBuffer);
                        byteBuffer.flip();
                        int len = byteBuffer.getInt();
                        System.out.println("数据包的长度:" + len);
                        int type = byteBuffer.getInt();
                        switch (type) {
                            case 1:
                                System.out.println("操作类型:server connect....");
                                break;
                            case 2:
                                System.out.println("操作类型:server create....");
                                break;
                            default:
                                break;
                        }
                        selectionKey.attach(type);
                        selectionKey.interestOps(SelectionKey.OP_WRITE);
                    } else if (selectionKey.isWritable()) {
                        Object attachment = selectionKey.attachment();
                        int type = (int) attachment;
                        SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
                        ByteBuffer lenBuffer = ByteBuffer.allocate(4);
                        ByteBuffer dataBuffer = ByteBuffer.allocate(1024);
                        ByteBuffer wrap;
                        switch (type) {
                            case 1:
                                System.out.println("isWritable:server connect....");
                                wrap = ByteBuffer.wrap("connect ok!".getBytes());
                                // 数据长度
                                lenBuffer.putInt(wrap.array().length);
                                lenBuffer.flip();
                                socketChannel.write(lenBuffer);

                                dataBuffer.put(wrap);
                                dataBuffer.flip();
                                break;
                            case 2:
                                System.out.println("isWritable:server create....");
                                wrap = ByteBuffer.wrap("create ok!".getBytes());
                                // 数据长度
                                lenBuffer.putInt(wrap.array().length);
                                lenBuffer.flip();
                                socketChannel.write(lenBuffer);

                                dataBuffer.put(wrap);
                                dataBuffer.flip();
                                break;
                            default:
                                break;
                        }
                        socketChannel.write(dataBuffer);
                        selectionKey.interestOps(SelectionKey.OP_READ);
                        selector.wakeup();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
            selectionKeys.clear();
        }
    }
}